//
//  ZDNewFeatureViewController.m
//  OCSinaWeiBo
//
//  Created by Apple on 15/10/30.
//  Copyright © 2015年 itcast. All rights reserved.
//

#import "ZDNewFeatureViewController.h"
#import <Masonry/Masonry.h>
#import "ZDTabBarController.h"

#define ZDItemCount 4

extern NSString *const ZDSwitchControllerNotification;

@interface ZDNewFeatureViewController ()

@property (nonatomic, strong) UICollectionViewFlowLayout *layout;

@end

@implementation ZDNewFeatureViewController

//重写构造方法 设置layout
- (instancetype)init
{
    UICollectionViewFlowLayout *layout = self.layout;
    
    return [super initWithCollectionViewLayout:layout];
}


static NSString * const reuseIdentifier = @"Cell";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self.collectionView registerClass:[ZDNewFeatureCell class] forCellWithReuseIdentifier:reuseIdentifier];
    
    //设置layout
    [self setupLayout];
}

#pragma mark - 设置布局参数
- (void)setupLayout
{
    self.layout.itemSize = [UIScreen mainScreen].bounds.size;
    self.layout.minimumLineSpacing = 0;
    self.layout.minimumInteritemSpacing = 0;
    
    //设置水平滚动
    self.layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    //设置分页
    self.collectionView.pagingEnabled = YES;
    
    //隐藏水平滚动条
    self.collectionView.showsHorizontalScrollIndicator = NO;
    
    //取消弹簧效果
    self.collectionView.bounces = NO;
}


#pragma mark - 数据源方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return ZDItemCount;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZDNewFeatureCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    cell.featureImageIndex = indexPath.item;

    return cell;
}

//该方法在cell当前cell消失，下一个cell完全显示的时候调用  传入的参数都是指的当前cell
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
    //获取当前可见item的indexPath
    NSIndexPath *idPath = self.collectionView.indexPathsForVisibleItems.lastObject;
    
    if (idPath.item == ZDItemCount - 1)
    {
        //根据indexPath获取当前显示cell
        ZDNewFeatureCell *featureCell = (ZDNewFeatureCell *)[self.collectionView cellForItemAtIndexPath:idPath];
        
        [featureCell startButtonAnimation];
    }
}


#pragma mark 懒加载
- (UICollectionViewFlowLayout *)layout
{
    if (!_layout)
    {
        _layout = [[UICollectionViewFlowLayout alloc] init];
    }
    
    return _layout;
}

@end



@interface ZDNewFeatureCell ()

@property (nonatomic, strong) UIImageView *featureImageView;

@property (nonatomic, strong) UIButton *startButton;

@end

@implementation ZDNewFeatureCell


- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        //添加子控件
        [self prepareUI];
    }
    
    return self;
}

//准备UI
- (void)prepareUI
{
    //添加子控件
    [self.contentView addSubview:self.featureImageView];
    [self.contentView addSubview:self.startButton];
    
    //添加约束
    [self.featureImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.left.bottom.right.mas_equalTo(self.contentView);
    }];
    
    [self.startButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(self.contentView);
        make.bottom.mas_equalTo(self.contentView.mas_bottom).offset(-160);
    }];
    
}

//设置索引
- (void)setFeatureImageIndex:(NSInteger)featureImageIndex
{
    _featureImageIndex = featureImageIndex;
    
    NSString *imageName = [NSString stringWithFormat:@"new_feature_%ld", self.featureImageIndex + 1];
    
    self.featureImageView.image = [UIImage imageNamed:imageName];
    
    //隐藏startButton
    self.startButton.hidden = YES;
}

//开始体验按钮动画
- (void)startButtonAnimation
{
    if (!self.startButton.hidden)
    {
        return;
    }
    //显示startButton
    self.startButton.hidden = NO;
    
    //设置一开始看不见
    self.startButton.transform = CGAffineTransformMakeScale(0, 0);
    
    [UIView animateWithDuration:1.0 delay:0.1 usingSpringWithDamping:0.6 initialSpringVelocity:0.5 options:0 animations:^{
        
        //恢复 还原
        self.startButton.transform = CGAffineTransformIdentity;
        
    } completion:^(BOOL finished) {
        
        
    }];
}


//开始体验按钮点击事件
- (void)startButtonClick
{
    [[NSNotificationCenter defaultCenter] postNotificationName:ZDSwitchControllerNotification object:self userInfo:@{@"class":[ZDTabBarController class]}];
}


#pragma mark 懒加载
- (UIImageView *)featureImageView
{
    if (!_featureImageView)
    {
        _featureImageView = [[UIImageView alloc] init];
        
//        ZDLog(@"%@", _featureImageView);
    }
    
    return _featureImageView;
}

- (UIButton *)startButton
{
    if (!_startButton)
    {
        _startButton = [[UIButton alloc] init];
        
        [_startButton setBackgroundImage:[UIImage imageNamed:@"new_feature_finish_button"] forState:UIControlStateNormal];
        [_startButton setBackgroundImage:[UIImage imageNamed:@"new_feature_finish_button_highlighted"] forState:UIControlStateHighlighted];
        
        [_startButton setTitle:@"开始体验" forState:UIControlStateNormal];
        
        [_startButton addTarget:self action:@selector(startButtonClick) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _startButton;
}

@end
